﻿#include "qtablewidgetex.h"
#include <QTime>
#include <QHeaderView>
#include "comfunc_qt.h"


QTableWidgetEx::QTableWidgetEx()
{
//    this->horizontalHeader()->setSectionResizeMode(0,QHeaderView::ResizeToContents);//某列宽自适应内容
//    this->horizontalHeader()->setSectionResizeMode(1,QHeaderView::ResizeToContents);//某列宽自适应内容
//    this->horizontalHeader()->setSectionResizeMode(2,QHeaderView::ResizeToContents);//某列宽自适应内容
//    this->horizontalHeader()->setSectionResizeMode(3,QHeaderView::ResizeToContents);//某列宽自适应内容

    int col = 0;
    for(col = 0; col < this->horizontalHeader()->count(); col++)
    {
        this->horizontalHeader()->setSectionResizeMode(col, QHeaderView::ResizeToContents);//列宽自适应内容
    }

    this->horizontalHeader()->setStretchLastSection(true);//最后一列自动扩展
}

QTableWidgetEx::QTableWidgetEx(QWidget *parent)
    :QTableWidget(parent)//子类必须显式调用父类有参构造，否则父类的有参构造无法被调用
{

    Act_DelRow = new QAction(tr("删除选中行"), this);
    Act_ClearAll = new QAction(tr("清空"), this);

    menu=new QMenu(this);

    menu->addAction(Act_ClearAll); //Act_ClearAll
    menu->addAction(Act_DelRow); //添加菜单项Act_DelRow


    QObject::connect(this->Act_DelRow,SIGNAL(triggered()),
                     this,SLOT(on_Act_DelRow_triggered()));//右键菜单的动作
    QObject::connect(this->Act_ClearAll,SIGNAL(triggered()),
                     this,SLOT(on_Act_ClearAll_triggered()));//右键菜单的动作

}
void QTableWidgetEx::contextMenuEvent(QContextMenuEvent *e)
{
    menu->exec( e->globalPos() );//在光标位置处弹出菜单
}
void QTableWidgetEx::on_Act_DelRow_triggered()
{

    this->removeRow(this->currentRow());//删除选中的行

}
void QTableWidgetEx::on_Act_ClearAll_triggered()
{
    this->setRowCount(0);//全删
}
void QTableWidgetEx::append_hex_string_to_last_row(QString send_or_rece, uint8_t buf[], uint16_t len)
{
    int rowCount = this->rowCount();
    this->setRowCount(rowCount+1);
    rowCount = this->rowCount();



    QString frameStr;
    byte_num_to_hex_string_with_space(frameStr, buf, len);


    QString timeStr = QString("%1:%2:%3.%4")
            .arg(QTime::currentTime().hour())
            .arg(QTime::currentTime().minute(),2,10,(QChar)'0')
            .arg(QTime::currentTime().second(),2,10,(QChar)'0')
            .arg(QTime::currentTime().msec(),2,10,(QChar)'0');


    this->setItem(rowCount-1, 0, new QTableWidgetItem(timeStr));//第1列：插入时间
    this->setItem(rowCount-1, 1, new QTableWidgetItem(send_or_rece));//第2列：插入方向
    this->setItem(rowCount-1, 2, new QTableWidgetItem(QString::number(len)));//第3列：插入字节数
    this->setItem(rowCount-1, 3, new QTableWidgetItem(frameStr));//第4列：插入报文

    buf[len] = '\0';
    this->setItem(rowCount-1, 4, new QTableWidgetItem(QString((const char *)buf)));//第4列：插入char报文


    if(send_or_rece == "已发送")
    {
        this->item(rowCount-1, 1)->setForeground(QColor(0, 0, 140));//rgb(211, 230, 255)
        this->item(rowCount-1, 2)->setForeground(QColor(0, 0, 140));//成功发送的报文:蓝色
        this->item(rowCount-1, 3)->setForeground(QColor(0, 0, 140));//成功发送的报文:蓝色
    }
    else if(send_or_rece == "接收")
    {
        this->item(rowCount-1, 1)->setForeground(QColor(140,0,0));
        this->item(rowCount-1, 2)->setForeground(QColor(140,0,0));//接收的报文：红色
        this->item(rowCount-1, 3)->setForeground(QColor(140,0,0));//接收的报文：红色
    }



    this->setCurrentCell(rowCount-1,2,QItemSelectionModel::Deselect);//设置current行，但不选中
    this->scrollToBottom();

}
void QTableWidgetEx::append_hex_string_to_last_row(QString &dir_str,
                                                   QString &byte_cnt_str,
                                                   QString &frame_str,
                                                   QString &frame_char_str)
{
    int rowCount = this->rowCount();
    this->setRowCount(rowCount+1);
    rowCount = this->rowCount();

    QString timeStr = QString("%1:%2:%3.%4")
            .arg(QTime::currentTime().hour())
            .arg(QTime::currentTime().minute(),2,10,(QChar)'0')
            .arg(QTime::currentTime().second(),2,10,(QChar)'0')
            .arg(QTime::currentTime().msec(),2,10,(QChar)'0');


    this->setItem(rowCount-1, 0, new QTableWidgetItem(timeStr));//第1列：插入时间
    this->setItem(rowCount-1, 1, new QTableWidgetItem(dir_str));//第2列：插入方向
    this->setItem(rowCount-1, 2, new QTableWidgetItem(byte_cnt_str));//第3列：插入字节数
    this->setItem(rowCount-1, 3, new QTableWidgetItem(frame_str));//第4列：插入报文
    this->setItem(rowCount-1, 4, new QTableWidgetItem(frame_char_str));//第4列：插入报文的char str
}
